//
//  PBHomeVC.m
//  Pods
//
//  Created by 曹连华 on 2020/4/1.
//

#import "HomeVC.h"
#import "HomeViewModel.h"
#import "HomeTitleTVCellModel.h"
#import "PBHome-Swift.h"
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
#import <ServiceCode/ServiceCode.h>
#import <CLHComponents/CLHComponents.h>
@interface HomeVC () <UITableViewDelegate,CycleImageContentViewDelegate>

@property (nonatomic, strong) NavigationTitleView *titleView;
@property (nonatomic, strong) CycleImageContentView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HomeViewModel *viewModel;

@end

@implementation HomeVC

+ (void)load {
    RouterVO *vo = [RouterVO new];
    vo.tab = YES;
    vo.createdType = PBRouteObjectCreateByCode;
    vo.index = 0;
    vo.className = NSStringFromClass(self);
    vo.keyInfo = @"app首页";
    [[Router sharedInstance]registerRouterVO:vo withKey:@"home"];
    
    NativeCallVO *callVO = [[NativeCallVO alloc]init];
    callVO.block = ^id(id param) {
        return [HomeVC new];
    };
    callVO.keyInfo = @"获取app首页VC";
    [[Router sharedInstance] registerNativeCallVO:callVO withKey:@"homevc"];
    
}

- (NavigationTitleView *)titleView {
    if (!_titleView) {
        _titleView = [[NavigationTitleView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.width, UIScreen.screenType == PBScreenTypeNormal ? 64:88)];
    }
    return _titleView;
}
- (CycleImageContentView *)headerView {
    if (!_headerView) {
        _headerView = [[CycleImageContentView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.width, rateHeight(260))];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (HomeViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[HomeViewModel alloc]init];
    }
    return _viewModel;;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self.viewModel.dataSource;
        _tableView.estimatedRowHeight = 120;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBarHidden:YES];
    [self setNavigationBarHiddenPop:YES];

    if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
    //        self.automaticallyAdjustsScrollViewInsets = NO;
        }
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.titleView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.mas_equalTo(0);
    }];
    
    [self.headerView updateDataWithImageUrls:[self imageUrlArray]];
    
    self.tableView.mj_header = [LOTRefreshHeaderView headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataAction)];
    
    self.tableView.mj_footer = [LOTRefreshFooterView footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDataAction)];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeTitleTVCellModel *row = (HomeTitleTVCellModel *)[self.viewModel.dataSource rowModelAtIndex:indexPath];
    if ([row.routerKey isEqualToString:@"userInterface"])  {
        [self.navigationController pushViewController:[UserInterfaceVC new] animated:YES];
    } else {
        [self.viewModel didSelectedRowModelWithindexPath:indexPath];
    }
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.titleView scrollViewOffsetY:scrollView.contentOffset.y headerViewHeight:self.headerView.bounds.size.height];
}

#pragma amrk - CycleImageContentViewDelegate
- (void)pageView:(CycleImagePageView *)aPageView didSelectedPageAtIndex:(NSUInteger )index {

}

- (void)pageView:(CycleImagePageView *)pageView didChangeToIndex:(NSUInteger )index {
    
}


- (NSArray <NSString *>*)imageUrlArray {
    return @[
             @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559735350095&di=3ddbfcf48f81591b6e31b1f0247a8042&imgtype=0&src=http%3A%2F%2Fac-r.static.booking.cn%2Fimages%2Fhotel%2Fmax1024x768%2F987%2F98767654.jpg",
             @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559734730876&di=8138a1fd7c173923853efe524211fb50&imgtype=0&src=http%3A%2F%2Fpic31.nipic.com%2F20130728%2F3822951_140613063000_2.jpg",
             @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1343015815,2335192405&fm=26&gp=0.jpg",
             @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559735350094&di=6da7b32b91cc1079fddfc98315719b6f&imgtype=0&src=http%3A%2F%2Fimg34.ddimg.cn%2Fimgother1%2F98%2F4%2F60038054_2.jpg",
             @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559735350094&di=558ca6692699a399417916b33980307c&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F458af12108c4da0f1cf4fe8e2713a458898e332b28196-jdjJ3j_fw658"];
}

- (void)refreshDataAction {
    WEAK_SELF;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        STRONG_SELF;
        [self.tableView.mj_header endRefreshing];
    });
}
- (void)loadMoreDataAction {
    WEAK_SELF;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        STRONG_SELF;
        [self.tableView.mj_footer endRefreshing];
    });
}

@end
